import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {ConfigProvider} from "../../providers/config/config";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/bond/refund',
    defaultHistory:['MemberPage']
})
@Component({
    selector: 'page-bond-refund',
    templateUrl: 'bond-refund.html',
})
export class BondRefundPage {
    postData = {
        withdraw_money: '',
        token: ''
    };
    frozen_money: any;
    token: string;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public config: ConfigProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
    }

    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillEnter(){
        this.token = this.config.getToken();
        this.postData = {
            withdraw_money: '',
            token: this.token
        };
        this.getBond();
    }

    //获取保证金金额：
    getBond() {
        this.httpService.requestPostData('/echarge/residue_recharge_frozen', {token: this.token}, response => {
            let error = response['error'];//错误代码
            let message = response['message'];//错误信息
            if (error === 200) {
                let data = response['data'];
                this.frozen_money = data['frozen_money'];
            } else {
                this.util.presentAlert(message);
            }
        });
    }

    //退还保证金
    doRefund() {
        this.httpService.requestPostData('/echarge/withdraw_frozen', this.postData, response => {
            this.navCtrl.push('BondRefundResultPage', response);
        });
    }
}
